/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.business.service.impl;

import me.zhengjie.modules.business.domain.ExPointDO;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.business.repository.ExPointRepository;
import me.zhengjie.modules.business.service.ExPointService;
import me.zhengjie.modules.business.service.dto.ExPointDto;
import me.zhengjie.modules.business.service.dto.ExPointQueryCriteria;
import me.zhengjie.modules.business.service.mapstruct.ExPointMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author ljj
* @date 2021-02-23
**/
@Service
@RequiredArgsConstructor
public class ExPointServiceImpl implements ExPointService {

    private final ExPointRepository exPointRepository;
    private final ExPointMapper exPointMapper;

    @Override
    public Map<String,Object> queryAll(ExPointQueryCriteria criteria, Pageable pageable){
        Page<ExPointDO> page = exPointRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(exPointMapper::toDto));
    }

    @Override
    public List<ExPointDto> queryAll(ExPointQueryCriteria criteria){
        return exPointMapper.toDto(exPointRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ExPointDto findById(Integer id) {
        ExPointDO exPoint = exPointRepository.findById(id).orElseGet(ExPointDO::new);
        ValidationUtil.isNull(exPoint.getId(),"ExPoint","id",id);
        return exPointMapper.toDto(exPoint);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExPointDto create(ExPointDO resources) {
        return exPointMapper.toDto(exPointRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ExPointDO resources) {
        ExPointDO exPoint = exPointRepository.findById(resources.getId()).orElseGet(ExPointDO::new);
        ValidationUtil.isNull( exPoint.getId(),"ExPoint","id",resources.getId());
        exPoint.copy(resources);
        exPointRepository.save(exPoint);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            exPointRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ExPointDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ExPointDto exPoint : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("实验ID", exPoint.getExId());
            map.put("是否默认指标", exPoint.getDefaultPoint());
            map.put("名称", exPoint.getName());
            map.put("说明", exPoint.getRemark());
            map.put("0: 优化指标，1：辅助指标，2：反向指标。", exPoint.getType());
            map.put("是否关键指标（用于显示在界面上）", exPoint.getMajor());
            map.put("计算表达式", exPoint.getCalcExpression());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}